package com.droid.lib.controls.demo.widget.tree.demo;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.demo.widget.tree.TreeNode;

/**
 * Created by Administrator on 2016/9/27.
 */
public class TreeItemHolder extends TreeNode.BaseNodeViewHolder<TreeItemHolder.TreeItem> {
    private TextView mtvText;
    private ImageView mivIcon;
    private ImageView mivArray;
    private boolean isSelected;
    private LinearLayout mllTreeItem;

    public TreeItemHolder(Context context) {
        super(context);
    }

    @Override
    public View createNodeView(final TreeNode node, TreeItem value) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.tree_item_holder, null, false);
        mtvText = (TextView) view.findViewById(R.id.tv_text);
        mivIcon = (ImageView) view.findViewById(R.id.iv_icon);
        mivArray = (ImageView) view.findViewById(R.id.iv_array);
        mllTreeItem = (LinearLayout) view.findViewById(R.id.ll_treeitem);
        if (value.icon != null) {
            mivIcon.setImageDrawable(value.icon);
        }
        mtvText.setText(value.text);
        //if My computer
        if (node.getLevel() == 0) {
            view.findViewById(R.id.iv_del).setVisibility(View.GONE);
        }
        view.findViewById(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (node.getLevel() == 1) {
                    TreeNode file1 = new TreeNode("孙敏test").setViewHolder(new TreeChildHolder(context));
                    getTreeView().addNode(node, file1);
                } else if (node.getLevel() == 2) {
                    TreeNode file1 = new TreeNode("王为娟test").setViewHolder(new TreeChildHolder(context));
                    getTreeView().addNode(node, file1);
                }

            }
        });
        mivArray.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                node.setSelected(!node.isSelected());
                for (TreeNode n : node.getChildren()) {
                    getTreeView().selectNode(n, !node.isSelected());
                }
            }
        });
        view.findViewById(R.id.iv_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTreeView().removeNode(node);
            }
        });
        return view;
    }

    @Override
    public void toggleSelectionMode(boolean editModeEnabled) {
        isSelected = editModeEnabled;
        mivArray.setVisibility(editModeEnabled ? View.VISIBLE : View.GONE);
        mivArray.setImageDrawable(mNode.isSelected() ? context.getResources().getDrawable(R.drawable.checkbox_checked) : context.getResources().getDrawable(R.drawable.checkbox_normal));
        mllTreeItem.setVisibility(editModeEnabled ? View.VISIBLE : View.GONE);
    }

    @Override
    public void toggle(boolean active) {
        mivArray.setImageDrawable(active ? context.getResources().getDrawable(R.drawable.checkbox_checked) : context.getResources().getDrawable(R.drawable.checkbox_normal));
    }

    public static class TreeItem {
        public Drawable icon;
        public String text;

        public TreeItem(Drawable icon, String text) {
            this.icon = icon;
            this.text = text;
        }
    }
}
